package com.ywgt.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.system.mapper.*;
import com.ywgt.system.pojo.*;
import com.ywgt.system.service.OutStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.system.vo.OutStockVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@Service
public class OutStockServiceImpl extends ServiceImpl<OutStockMapper, OutStock> implements OutStockService {
    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private OutStockMapper outStockMapper;

    @Autowired
    private OutStockInfoMapper outStockInfoMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductStockMapper productStockMapper;

    @Override
    public boolean consumerInsert(OutStockVO outStockVO,AuthUser authUser,Long consuemrId) {
        String outNum= UUID.randomUUID().toString().substring(0,20);
        OutStock outStock=new OutStock();
        BeanUtils.copyProperties(outStockVO,outStock);
        outStock.setOutNum(outNum);
        outStock.setOperator(authUser.getUsername());
        outStock.setConsumerId(consuemrId);
        List<Integer> counts = outStockVO.getProducts().stream().map(Product::getCount).collect(Collectors.toList());
        int num=0;
        for (Integer count : counts) {
            num+=count;
        }
        outStock.setProductNumber(num);
        outStockMapper.insert(outStock);
        List<Long> ids = outStockVO.getProducts().stream().map(Product::getId).collect(Collectors.toList());
        List<Product> products = productMapper.selectBatchIds(ids);
        for (Product product : products) {
            for (Product outStockVOProduct : outStockVO.getProducts()) {
                if (outStockVOProduct.getId()==product.getId()){
                    product.setCount(outStockVOProduct.getCount());
                }
            }
        }
        OutStockInfo outStockInfo=new OutStockInfo();
        BeanUtils.copyProperties(outStockVO,outStockInfo);
        outStockInfoMapper.saveBatch(outNum,products);
        productStockMapper.decereCount(products);
        return true;
    }

    public boolean newInsert(OutStockVO outStockVO, AuthUser authUser){
        Consumer consumer=new Consumer();
        BeanUtils.copyProperties(outStockVO,consumer);
        consumerMapper.save(consumer);
        return consumerInsert(outStockVO,authUser,consumer.getId());
    }

    @Override
    public OutStock detail(Long id) {
        return outStockMapper.detail(id);
    }

    @Override
    public Page<OutStockVO> findOutStockList(Integer pageNum, Integer pageSize, OutStockVO outStockVO) {
        PageHelper.startPage(pageNum,pageSize);
       return outStockMapper.findOutStockList(outStockVO);
    }
}
